<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
       $this->result = $this->getCateByPid();
       $this->liebiao = U('liebiao'); 
       $this->assign('registerform',U('userright/registerform'));  
       $this->display();
        
    }

    //递归目录
  	public function getCateByPid($pid=0){
  		$category = M('category');
          $result = $category->where("status=1 AND pid='{$pid}'")->select();
          if(!empty($result)){
              foreach ($result as $key => $value) {
                  $result[$key]['child']=$this->getCateByPid($value['id']);
              }
          }       
          return $result;
      }


     public function liebiao(){ 
       $id = I('get.id');
       $order = I('get.order');
       if ($id>0) {
         $productModel = M('product');
         // $result = $productModel->where("category_id='{$id}'")->select();
         // foreach ($result as $key => $value) {         
         // }
         if($order =='price'){
           $result= $productModel->join('fm_product_to_category as cate ON fm_product.id=cate.product_id')->where("cate.category_id={$id}")->order("price ASC")->select();
         }else{
           $result= $productModel->join('fm_product_to_category as cate ON fm_product.id=cate.product_id')->where("cate.category_id={$id}")->select();
         }
       
        $this->result = $result;
        $count= $productModel->join('fm_product_to_category as cate ON fm_product.id=cate.product_id')->where("cate.category_id={$id}")->count();
        $this->count = $count;
        $Page = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page -> setConfig('header','共%TOTAL_ROW%条');
        $Page -> setConfig('first','首页');
        $Page -> setConfig('last','共%TOTAL_PAGE%页');
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $Page -> setConfig('link','indexpagenumb');//pagenumb 会替换成页码
        $Page -> setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $this -> page = $Page->show();
        $categoryModel = M('category');
        $r = $categoryModel->where("id={$id}")->find();
        $this->r=$r;
        $this->id=$id;  
        $this->assign('show',U('show'));     
        $this->display();       
      }else{
        $this->error('产品还没上架');
      } 
    }


      public function show(){
       $id = I('get.id'); 
       if ($id>0) {                
         $productModel = M('product');
         $row = $productModel->where("id={$id}")->find();
           $this->product = $row;
           $productImgModel = M('product_image');
           $result = $productImgModel->where("product_id={$id}")->select();
           $this->productImage = $result;
           $this->assign('show',U('show'));
           $this->assign('address',U('userright/address'));
           $this->assign('gouwuche',U('userright/addGouwuche'));
           $this->display();
       } 
    }


 }